import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘柄华
 * Date: 2022-06-07
 * Time: 21:31
 */

//删除链表中重复的结点
// 在一个排序的链表中，存在重复的结点，请删除该链表中重复的结点，重复的结点不保留，返回链表头指针
public class TestDemo02{
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead == null)return pHead;

        ListNode head = new ListNode(0);
        head.next = pHead;
        ListNode prev = head;
        ListNode last = head.next;
        while(last!=null){
            while(last.next!=null  && last.val!=last.next.val){
                prev = prev.next;
                last = last.next;
            }
            while(last.next!=null && last.val == last.next.val){
                last = last.next;
            }
            if(prev.next != last){
                prev.next = last.next;
            }
            last = last.next;
        }
        return head.next;
    }
}


 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}